#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<int> pancakeSort(vector<int>& arr) {
        vector<int> arrTmp = arr;
        vector<int> ret{};

        while(arrTmp.size() != 1) {
            auto it = max_element(arrTmp.begin(), arrTmp.end());
            auto pos = it - arrTmp.begin();
            if (pos == arrTmp.size() - 1) {
                arrTmp.pop_back();
            } else if (pos == 0) {
                ret.push_back(arrTmp.size());
                reverse(arrTmp.begin(), arrTmp.end());
                arrTmp.pop_back();
            } else {
                ret.push_back(pos + 1);
                reverse(arrTmp.begin(), arrTmp.begin() + pos + 1);
            }
        }

        return ret;
    }
};


int main()
{
    vector<int> vt{1, 2, 3, 4, 5};
    Solution s;
    vector<int> ret = s.pancakeSort(vt);

    for (const auto& elem : ret) {
        cout << elem << " ";
    }
    cout << endl;

    return 0;
}